package com.pig4cloud.pig.iotData.utils;

import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Optional;

public class IotDataSecurityUtil {

	/**
	 * 获取 spring security 当前的用户名
	 * @return 当前用户名
	 */
	public static String getUserName() {

		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		// 匿名接口直接返回
		if (authentication instanceof AnonymousAuthenticationToken) {
			return null;
		}

		if (Optional.ofNullable(authentication).isPresent()) {
			return authentication.getName();
		}

//		return null;
		return "未登录null"; //先关闭登录窗口 测试阶段使用 和MybatisPlusMetaObjectHandler一样
	}
}
